using System;
using System.Data;
using MyClever.Lib;

namespace MyClever.DefaultPlugins
{
    public class AddColumnExpression : MissionPlugin
    {
        [Package(Direction.Input, "Defines the origin package where you like add new expression columns.")]
        enum OriginPackage { }

        [Package(Direction.Input, "Defines the expression columns you like to add.")]
        enum ExpressionColumns
        {
            [Column(typeof(string), "The new name of the expression column.")]
            ColumnName,
            [Column(typeof(string), "The expression which defines the value of the new column.")]
            ColumnType,
            [Column(typeof(string), "The expression which defines the value of the new column.")]
            ColumnExpression,
            [Column(typeof(bool), "If true, not the expression itself, but the result will saved only.")]
            SaveExpressionResultOnly
        }

        [Package(Direction.Output, "The new package with all expression columns.")]
        enum Output
        { }

        [Plugin(PluginDevStatus.Development, "Add a new column to the given packages, which calculates their value by calculating other column-values.")]
        public AddColumnExpression()
        {
        }

        public override void Initialize()
        {
            //This is the first Function called by the host...
            //Put anything needed to start with here first
        }

        public override void Dispose()
        {
            //Put any cleanup code in here for when the program is stopped
        }

        protected override void Work()
        {
            MissionPluginPackage outputPackage = this.GetPackageByName(typeof(Output));
            outputPackage.CopyDataFromPackage(this.GetPackageByName(typeof(OriginPackage)));

            DataTableReader originPackageReader = this.GetDataReader(typeof(ExpressionColumns));
            while (originPackageReader.Read())
            {
                this.AddNewLoggingMessage(typeof(ExpressionColumns), String.Format("add new column {0} with expression:{1}", originPackageReader[ExpressionColumns.ColumnName.ToString()].ToString(), originPackageReader[ExpressionColumns.ColumnExpression.ToString()].ToString()));

                MissionPluginPackageColumn newTmpColumn = new MissionPluginPackageColumn(originPackageReader[ExpressionColumns.ColumnName.ToString()].ToString(), Type.GetType(originPackageReader[ExpressionColumns.ColumnType.ToString()].ToString()));
                newTmpColumn.Expression = originPackageReader[ExpressionColumns.ColumnExpression.ToString()].ToString();
                outputPackage.Columns.Add(newTmpColumn);

                if (true == Convert.ToBoolean(originPackageReader[ExpressionColumns.SaveExpressionResultOnly.ToString()]))
                {
                    newTmpColumn.ColumnName = new Guid().ToString();
                    
                    MissionPluginPackageColumn newColumn = new MissionPluginPackageColumn(originPackageReader[ExpressionColumns.ColumnName.ToString()].ToString(), Type.GetType(originPackageReader[ExpressionColumns.ColumnType.ToString()].ToString()));
                    outputPackage.Columns.Add(newColumn);

                    foreach (DataRow rowOn in outputPackage.Rows)
                    {
                        rowOn[newColumn] = rowOn[newTmpColumn];
                    }

                    outputPackage.Columns.Remove(newTmpColumn);
                }
            }
        }
    }
}
